<%@page import="com.liferay.portal.kernel.language.LanguageUtil"%>
<%@page import="com.liferay.portal.service.permission.PortletPermissionUtil" %>
<%@page import="com.liferay.portal.kernel.util.WebKeys"%>
<%@page import="com.liferay.portal.theme.ThemeDisplay"%>
<%@page import="javax.portlet.PortletURL"%>
<%@page import="java.util.List"%>
<%@page import="com.liferay.portal.kernel.util.ListUtil"%>
<%@page import="com.liferay.portal.service.OrganizationLocalServiceUtil"%>
<%@page import="com.liferay.portal.model.Organization" %>

<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="doan.quanlycanbo.portlet.model.donvi" %>
<%@page import="doan.quanlycanbo.portlet.service.donviLocalServiceUtil" %>

<portlet:defineObjects/>
<liferay-theme:defineObjects />

<style>
	.a{
		padding-right: 20px;
		padding-left: 0px;
	}
</style>

<%

	PortletURL rowUrl = renderResponse.createRenderURL();
	rowUrl.setParameter("jspPage","/html/manage_edirectory/department/sub_department.jsp");
	
	ThemeDisplay theme1 = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
	long groupId = theme1.getScopeGroupId();
	
%>

<portlet:renderURL windowState="normal" var="addDepartmentURL">
	<portlet:param name="jspPage" value="/html/manage_employee/department/add_department.jsp"></portlet:param>
</portlet:renderURL> 
<br/>
<c:if test='<%= PortletPermissionUtil.contains(permissionChecker, portletDisplay.getId(), "ADD_FUNCTION") %>'>
<input type="button" value='<%= LanguageUtil.get(pageContext, "managecontacts.agency.view-button.addagency","Them Don Vi") %>' onClick="window.location.href='<%= addDepartmentURL.toString() %>' "/>
</c:if>
<br/><br/><br/>

<!--  Danh sách đơn vị -->
<%-- <portlet:renderURL var="renderTabDepartmentURL"> --%>
<%-- 	<portlet:param name="jspPage" value="/html/manage_edirectory/view.jsp?tab1=tab.danhba.donvi"/> --%>
<%-- </portlet:renderURL> --%>
<%
	PortletURL renderTabDepartmentURL = renderResponse.createRenderURL();
	renderTabDepartmentURL.setParameter("jspPage", "/html/manage_employee/view.jsp?tab1=tab.qlcb.donvi");
%>
<%-- <liferay-ui:search-container emptyResultsMessage="Ko co du lieu" delta="15" iteratorURL="<%= renderTabDepartmentURL %>" > --%>
<%-- 	<liferay-ui:search-container-results> --%>
<%-- 		<% --%>
<!-- // 			long[] organizationIds = new long[]{12403L}; -->
<!-- // 			List<Organization> orgList = OrganizationLocalServiceUtil.getOrganizations(organizationIds); -->
<!-- // 			List<Organization> subOrgLv1List = OrganizationLocalServiceUtil.getSuborganizations(orgList); -->
			
<!-- //  			List<donvi> donviList = donviLocalServiceUtil.getByParent(0); -->
<!-- //  			results = ListUtil.subList(subOrgLv1List, searchContainer.getStart(), searchContainer.getEnd()); -->
<!-- //  			total = subOrgLv1List.size(); -->
<!-- // 			pageContext.setAttribute("results", results); -->
<!-- // 			pageContext.setAttribute("total", total); -->
<%-- 		%> --%>
<%-- 	</liferay-ui:search-container-results> --%>
	
<%-- 	<liferay-ui:search-container-row className="com.liferay.portal.model.Organization" keyProperty="organizationId" modelVar="org"> --%>
<%-- 		<% --%>
<!-- //  			StringBuffer name = new StringBuffer(); -->
<!-- //  			name.append("<a style='text-decoration:none'><b>"+org.getName()+"</b></a><br/>"); -->
<!-- //  			long orgId = org.getOrganizationId(); -->
<!-- //  			long[] organizationIdsLv2 = new long[]{orgId}; -->
<!-- //  			List<Organization> orgListLv2 = OrganizationLocalServiceUtil.getOrganizations(organizationIdsLv2); -->
<!-- //  			List<Organization> subOrgLv2List = OrganizationLocalServiceUtil.getSuborganizations(orgListLv2);	 -->
<!-- //  			List<donvi> donviconList = donviLocalServiceUtil.getByParent(donvi.getMadonvi()); -->
<!-- //  			if(subOrgLv2List.size() > 0){ -->
<!-- //  				name.append("<img class='a' src='/QuanLyCanBo-portlet/html/manage_edirectory/department/images/children.gif'/>"); -->
<!-- //  				name.append("<a style = 'text-decoration: underline'>"+"Phòng ban "+":</a>"); -->
<!-- //  				for(Organization orgLv2 : subOrgLv2List){ -->
<!-- //  					rowUrl.setParameter("parentId", String.valueOf(orgLv2.getOrganizationId())); -->
<!-- //  					name.append("<a style = 'text-decoration:none' onclick='window.location.href=\""+rowUrl.toString()+"\"'>"+orgLv2.getName()+"</a>,"); -->
<!-- //  				} -->
<!-- //  				name.deleteCharAt(name.length() - 1); -->
<!-- //  			} -->
<!-- 		%> -->
<%-- 		<liferay-ui:search-container-column-text name='<%=LanguageUtil.get(pageContext, "managecontacts.agency.view-title.table.agency","Ten don vi") %>' value='<%= name.toString() %>'/> --%>
<%-- 		<liferay-ui:search-container-column-text name="Don vi cha" value="<%= org.getParentOrganization().getName() %>"/>	 --%>
<%-- 		<liferay-ui:search-container-column-jsp	path="/html/manage_employee/department/actions_department.jsp" align="right" /> --%>
<%-- 	</liferay-ui:search-container-row> --%>
<%-- 	<liferay-ui:search-iterator searchContainer="<%= searchContainer %>" /> --%>
<%-- </liferay-ui:search-container> --%>

<%-- <img src="<%= request.getContextPath() + "/department/CuocDoiSV.png"%>" alt="ko co anh" /> --%>


<liferay-ui:search-container emptyResultsMessage="Ko co du lieu" delta="15" iteratorURL="<%= renderTabDepartmentURL %>" >
	<liferay-ui:search-container-results>
		<%
			List<donvi> donviList = donviLocalServiceUtil.getByParent(0);
			results = ListUtil.subList(donviList, searchContainer.getStart(), searchContainer.getEnd());
			total = donviList.size();
			pageContext.setAttribute("results", results);
			pageContext.setAttribute("total", total);
		%>
	</liferay-ui:search-container-results>
	
	<liferay-ui:search-container-row className="doan.quanlycanbo.portlet.model.donvi" keyProperty="madonvi" modelVar="donvi">
		<%
			StringBuffer name = new StringBuffer();
			name.append("<a style='text-decoration:none'><b>"+donvi.getTendonvi()+"</b></a><br/>");
			List<donvi> donviconList = donviLocalServiceUtil.getByParent(donvi.getMadonvi());
			if(donviconList.size() > 0){
				name.append("<img class='a' src='/QuanLyCanBo-portlet/html/manage_edirectory/department/images/children.gif'/>");
				name.append("<a style = 'text-decoration: underline'>"+"Đơn vị cấp dưới "+":</a>");
				for(donvi dvc : donviconList){
					rowUrl.setParameter("parentId", String.valueOf(donvi.getMadonvi()));
					name.append("<a style = 'text-decoration:none' onclick='window.location.href=\""+rowUrl.toString()+"\"'>"+dvc.getTendonvi()+"</a>,");
				}
				name.deleteCharAt(name.length() - 1);
			}
		%>
		<liferay-ui:search-container-column-text name='<%=LanguageUtil.get(pageContext, "managecontacts.agency.view-title.table.agency","Ten don vi") %>' value='<%= name.toString() %>'/>
		<liferay-ui:search-container-column-jsp	path="/html/manage_employee/department/actions_department.jsp" align="right" />
	</liferay-ui:search-container-row>
	<liferay-ui:search-iterator searchContainer="<%= searchContainer %>" />
</liferay-ui:search-container>

